<?php
namespace app\admin\controller;


use app\common\model\Menu;
use app\common\model\Roleauthorities;


/**
 * 企业管理
 * Class Realdata
 * @package app\gygt\controller
 */
class Sysmenu extends Admin
{
    /**
     *添加
     */
    public function add_or_save(){
        $data['menu_id'] = input('menu_id');
        $data['menu_name'] = input('menu_name','','trim');
        $data['parent_id'] = input('parent_id',0);
        $data['menu_url']  = input('menu_url','','trim');
        $data['menu_icon']  = input('menu_icon','','trim');
        $data['sort_number']  = input('sort_number');
        $data['ismenu']  = input('ismenu',2);  //1-是按钮  2-是菜单

        $model =new Menu();

        //验证用户是否存在
        if(!$data['menu_id']){
            $result = $this->validate($data, 'SysMenu.add');
            if ($result !== true) return json_msg($result);

            $data['create_time'] = date('Y-m-d H:i:s');
            $res = $model->insert($data);
            $msg = '添加成功';
        }else{
            $result = $this->validate($data, 'SysMenu.update');
            if ($result !== true) return json_msg($result);

            $data['update_time'] = date('Y-m-d H:i:s');
            $res = $model->save($data,['menu_id'=>$data['menu_id']]);
            $msg = '修改成功';
        }
        json_success_msg($msg);
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('menu_id');
        $model =new Menu();
        //1 参数验证是否合法
        if (empty($id)){
            json_error_msg("菜单ID不能为空！请检查");
        }
        $con['menu_id'] = $id;
        $res = $model->where($con)->delete();
        //权限表同样做删除操作
        $roleMenuModel = new Roleauthorities();
        $roleMenuModel->where($con)->delete();
        json_success_msg('删除成功');
    }


    public function detail(){
        $id = input('menu_id');
        $model =new \app\common\model\Menu();
        //1 参数验证是否合法
        if (empty($id)){
            json_error_msg("ID不能为空！请检查");
        }
        $con['menu_id'] = $id;
        $res = $model->where($con)->find();
        json_success_msg($res);
    }

    /**
     * 只有菜单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lst(){
        $word = input('keyword','','trim');
        $model =new Menu();
        $param = [];
        if($word){
            $param['menu_name'] = ['like','%'.$word.'%'];
        }

        $param['ismenu'] = 2;
        $count = $model->where($param)->count();
        $lst = $model->with('parent')->where($param)->order('sort_number')->select();
        return json_tab_msg($count,1,$lst);
    }

    /**
     * 获取所有菜单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllMenu(){
        $model = new Menu();
        $list = $model->field('menu_id,menu_name')->where('ismenu',2)->select();
        json_success_msg($list);
    }

    /**
     * 所有权限
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function auth_lst(){
        $role_id = input('role_id');
        $word = input('search_word','','trim');
        if(empty($role_id)){
            json_error_msg("角色ID不能为空，请检查！");
        }
        $model =new Menu();

        $param = [];
        if($word){
            $param['menu_name'] = ['like','%'.$word.'%'];
        }
        $param['ismenu'] = 2;
        $lst = $model->with('parent')->where($param)->order('sort_number')->select();


        //获取是否选中
        $role_auth_model = new Roleauthorities();
        $role_auth_lst = $role_auth_model->where(['role_id'=>$role_id])->column('menu_id');


        foreach ($lst as &$temp){
            $temp['checked'] = 0 ;
            if(in_array($temp['menu_id'],$role_auth_lst)){
                $temp['checked'] = 1;
            }
        }
        json_success_msg($lst);
    }



    /**
     * 保存权限角色
     */
    public function add_save_auth(){
        $role_id = input('role_id');
        $auths = input('authids');
        if(empty($role_id)){
            json_error_msg("角色ID不能为空，请检查！");
        }
        $role_auth_model = new Roleauthorities();
        $role_auth_model->where(array('role_id'=>$role_id))->delete();

        $autharr = explode(',',$auths);
        $date = date('Y-m-d H:i:s');
        foreach ($autharr as $temp){
            $data = array();
            $data['role_id'] = $role_id;
            $data['menu_id'] = $temp;
            $data['create_time'] = $date;
            $role_auth_model->insert($data);
        }
        json_success_msg(array('count'=>count($autharr)));

    }

}
